import {Component, OnInit, ViewChild} from '@angular/core';
import {NavController, ToastController} from '@ionic/angular';
import {BasePage} from '../../common/basePage';
import {ActivatedRoute} from '@angular/router';
import {HttpService} from '../../services/http.service';
import {UrlAndOPT} from '../../common/url-and-opt.enum';
import {Observable, of} from 'rxjs/index';

/**
 * 交易记录
 */
@Component({
  selector: 'app-user-records',
  templateUrl: './user-records.page.html',
  styleUrls: ['./user-records.page.scss'],
})
export class UserRecordsPage extends BasePage implements OnInit {

  @ViewChild('infiniteScroll') infiniteScroll;
  // 分页及ID参数
  public condition: any = {
    userRole: '1', // 用户当前角色
    currPage: 1, // 当前页码
    pageSize: 20 // 每页数据条数
  };
  public dataList: any[] = [];
  public title = '交易记录';
  public recordOPT: number = UrlAndOPT.auth_transaction_record;
  public isAll = true;
  public typeObj = {
    all: {
      title: '交易记录',
      OPT: UrlAndOPT.auth_transaction_record
    },
    recharge: {
      title: '充值记录',
      OPT: UrlAndOPT.auth_recharge_record
    },
    withdrawal: {
      title: '提现记录',
      OPT: UrlAndOPT.auth_withdrawal_record
    }
  };

  constructor(
    public navCtrl: NavController,
    public toastCtrl: ToastController,
    private routeInfo: ActivatedRoute,
    private service$: HttpService
  ) {
    super(navCtrl, toastCtrl);
  }

  ngOnInit() {
    this.condition['userRole'] = this.routeInfo.snapshot.params['userRole'];
    // 判断记录类型(交易、充值、提现)
    if (this.typeObj[this.routeInfo.snapshot.params['recordType']]) {
      this.recordOPT = this.typeObj[this.routeInfo.snapshot.params['recordType']]['OPT'];
      this.title = this.typeObj[this.routeInfo.snapshot.params['recordType']]['title'];
    }
    // 是否为交易记录
    this.isAll = (this.recordOPT === this.typeObj['all']['OPT']);
    this.getDataList()
      .subscribe(obj => {
        this.handler(obj);
      });
  }

  // 获取数据
  private getDataList(): Observable<any> {
    if (this.recordOPT) {
      return this.service$.doRequest(this.recordOPT, this.condition);
    }
    return of(false);
  }

  // 下拉刷新列表
  doRefresh(event?) {
    this.condition.currPage = 1;
    this.getDataList()
      .subscribe(obj => {
        this.handler(obj);
        if (event) {  // 如果为下拉刷新，关闭下拉组件
          event.target.complete();
        }
      });
  }

  // 处理数据
  private handler(obj) {
    if (obj && obj['records']) {
      const records = obj.records;
      this.condition = {...this.condition, currPage: records['currPage'], pageSize: records['pageSize']};
      this.dataList = records['page'];
      // 根据列表长度判断是否禁用加载
      this.infiniteScroll.disabled = this.dataList.length === records['totalCount'];
    } else {
      this.infiniteScroll.disabled = true;
    }
  }

  // 上拉加载更多
  loadMoreData(event) {
    this.condition.currPage++;
    this.getDataList()
      .subscribe(obj => {
        if (obj && obj['records']) {
          const records = obj.records;
          // 累加数据
          this.dataList = [...this.dataList, ...records['page']];
          // 根据列表长度判断是否禁用加载
          this.infiniteScroll.disabled = this.dataList.length === records['totalCount'];
        }
        // 关闭上拉组件
        event.target.complete();
      });
  }

  // 前往交易记录详情页，并传递详情信息
  pushDetail(data: any) {
    if (this.isAll) {
      this.navCtrl.navigateForward(['/auth/user-records-detail', {data: JSON.stringify(data)}]);
    }
  }

}
